package com.android.sgzcommon.sound;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import androidx.annotation.RawRes;

/**
 * @author sgz
 * @date 2021-03-01
 */
public class SoundManager {

    private static SoundManager sSoundManager;
    private Context mContext;
    private SoundPool mSoundPool;

    private SoundManager(Context context, int maxStreams, int streamType) {
        mContext = context;
        if (mSoundPool != null) {
            mSoundPool.release();
        }
        if (Build.VERSION.SDK_INT >= 21) {
            SoundPool.Builder builder = new SoundPool.Builder();
            builder.setMaxStreams(maxStreams);
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            attrBuilder.setLegacyStreamType(streamType);
            builder.setAudioAttributes(attrBuilder.build());
            mSoundPool = builder.build();
        } else {
            /**
             * 第一个参数：int maxStreams：SoundPool对象的最大并发流数
             * 第二个参数：int streamType：AudioManager中描述的音频流类型
             *第三个参数：int srcQuality：采样率转换器的质量。 目前没有效果。 使用0作为默认值。
             */
            mSoundPool = new SoundPool(maxStreams, streamType, 0);
        }
        mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

            }
        });
    }

    public static SoundManager getInstance(Context context) {
        synchronized (SoundManager.class) {
            if (sSoundManager == null) {
                sSoundManager = new SoundManager(context, 1, AudioManager.STREAM_MUSIC);
            }
        }
        return sSoundManager;
    }

    public static SoundManager getInstance(Context context, int maxStreams, int streamType) {
        synchronized (SoundManager.class) {
            if (sSoundManager == null) {
                sSoundManager = new SoundManager(context, maxStreams, streamType);
            }
        }
        return sSoundManager;
    }

    /**
     * @param resId
     * @return
     */
    public int load(@RawRes int resId) {
        return mSoundPool.load(mContext, resId, 1);
    }

    /**
     * @param resId
     * @param priority
     * @return
     */
    public int load(@RawRes int resId, int priority) {
        return mSoundPool.load(mContext, resId, priority);
    }

    public void play(int voiceId) {
        if (mSoundPool != null) {
            mSoundPool.play(voiceId, 1, 1, 1, 0, 1);
        }
    }

    public void play(int voiceId, float leftVolume, float rightVolume, int priority, int loop, float rate) {
        if (mSoundPool != null) {
            mSoundPool.play(voiceId, leftVolume, rightVolume, priority, loop, rate);
        }
    }

    /**
     *
     */
    public void release() {
        sSoundManager = null;
        if (mSoundPool != null) {
            mSoundPool.release();
        }
    }
}
